import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/time_util.dart';
import 'package:baselib/widgets/comm_app_bar.dart';
import 'package:baselib/widgets/gradient_card_view.dart';
import 'package:baselib/widgets/gradient_text_widget.dart';
import 'package:baselib/widgets/inkwell_button.dart';
import 'package:baselib/widgets/refresh_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/chat_message_audio_item.dart';
import 'package:framelib/widgets/chat_message_image_item.dart';
import 'package:framelib/widgets/chat_message_video_item.dart';
import 'package:framelib/widgets/comm_widget_manager.dart';
import 'package:framelib/widgets/emoji_panel.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:framelib/widgets/record_panel.dart';
import 'package:get/get.dart';
import 'package:module_business/module_chat/viewmodel/chat_view_model.dart';
import 'package:module_business/module_chat/viewmodel/system_notification_view_model.dart';
import 'package:module_business/module_comm/model/user_params_model.dart';
import 'package:module_business/module_index/viewmodel/index_view_model.dart';
import 'package:nim_core/nim_core.dart';
/**
 * 主页
 */
class SystemNotificationPage extends GetWidget<SystemNotificationViewModel> {

  SystemNotificationPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Get.put(SystemNotificationViewModel());
    return PageWidget.build(
        builder: (SystemNotificationViewModel model){
          return PageWidget.PageScaffoldWidget(
            isShowBg: true,
            appBar: CommAppBar(
              backgroundColor: ColorUtil.instance.color_transparent,
              titleStr: StringUtil.instance.System_Notion,
              titleTextStyle: TextStyle(
                  fontSize: PxUtil.instance.px_18,
                  color: ColorUtil.instance.color_white,
                  fontWeight: FontWeight.bold
              ),
            ),
            body: RefreshUtil.init(
              model.refreshController,
                  ()=>model.onRefresh(pageCur: model?.page=1),
                  null,
              Obx(() => _buildListUi(model)),
              physics: ClampingScrollPhysics(),
              loadStatus: model?.loadStatus,
              enablePullDown: true,
              enablePullUp: false,
              isCanMoreData: false,
              onReFreshFunction:()=>model.onRefresh(pageCur: model?.page=1),
            ),
          );
        }
    );
  }

  Widget _buildListUi(SystemNotificationViewModel model) {
    model.msgDatas.value = model.msgDatas.value.reversed.toList();
    return ListView.builder(
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemCount: model.msgDatas.value.length??0,
        itemBuilder: (BuildContext context,int index){
          NIMMessage msgDataItem = model.msgDatas.value[index];
          LogUtil.d('otherAccId--->${model.voicePersonYxAccid}');
          LogUtil.d('selfAccId--->${UserParamsModel.instance.yxAccid}');
          LogUtil.d('msgDataItem--->${msgDataItem?.toMap()}');
          if(msgDataItem?.fromAccount == UserParamsModel.instance.yxAccid){
            //自己
            return buildSelfMsgUi(model,msgDataItem);
          }else{
            //别人
            return buildOtherMsgUi(model,msgDataItem);
          };
        }
    );
  }

  Widget buildSelfMsgUi(SystemNotificationViewModel model, NIMMessage msgDataItem) {
    return Container(
      width: PxUtil.instance.screenWidth,
      margin: EdgeInsets.only(
        bottom: PxUtil.instance.px_30
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: PxUtil.instance.screenWidth/4,
          ),
          Expanded(child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              _buildChatItemUi(model,msgDataItem),
              SizedBox(height:  PxUtil.instance.px_5,),
              Text(
                '${TimeUtils.forMatTimeByMs(msgDataItem?.timestamp,format: 'yyyy-MM-dd HH:mm:ss')}',
                style: TextStyle(
                    fontSize: PxUtil.instance.px_8,
                    color: ColorUtil.instance.color_white
                ),
              )
            ],
          )),
          Container(
              margin: EdgeInsets.only(
                  left: PxUtil.instance.px_12,
                  right: PxUtil.instance.px_12
              ),
              child:
              CommWidgetManager.buildCicleImagWidget(
                UserParamsModel.instance.userEntity?.icon ?? '',
                placeholder:
                DrawableUtil.instance.icon_default_head,
                width: PxUtil.instance.px_32,
                height: PxUtil.instance.px_32,
                roundRadius: PxUtil.instance.px_16,
                backgroundColor: ColorUtil.instance.color_f2f2f2,
                padding: EdgeInsets.only(),
                margin: EdgeInsets.only(),
              )
          ),
        ],
      ),
    );
  }
  Widget buildOtherMsgUi(SystemNotificationViewModel model, NIMMessage msgDataItem) {
    return Container(
      width: PxUtil.instance.screenWidth*3/4,
      margin: EdgeInsets.only(
          bottom: PxUtil.instance.px_30
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
              margin: EdgeInsets.only(
                  left: PxUtil.instance.px_12,
                  right: PxUtil.instance.px_12
              ),
              child:
              CommWidgetManager.buildCicleImagWidget(
                model.voicePersonIcon ?? '',
                placeholder:
                DrawableUtil.instance.icon_default_head,
                width: PxUtil.instance.px_32,
                height: PxUtil.instance.px_32,
                roundRadius: PxUtil.instance.px_16,
                backgroundColor: ColorUtil.instance.color_f2f2f2,
                padding: EdgeInsets.only(),
                margin: EdgeInsets.only(),
              )
          ),
          Expanded(child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildChatItemUi(model,msgDataItem),
              SizedBox(height:  PxUtil.instance.px_5,),
              Text(
                '${TimeUtils.forMatTimeByMs(msgDataItem?.timestamp,format: 'yyyy-MM-dd HH:mm:ss')}',
                style: TextStyle(
                    fontSize: PxUtil.instance.px_8,
                    color: ColorUtil.instance.color_white
                ),
              )
            ],
          )),
          SizedBox(
            width: PxUtil.instance.screenWidth/4,
          ),
        ],
      ),
    );
  }

  _buildChatItemUi(SystemNotificationViewModel model, NIMMessage msgDataItem) {
    switch (msgDataItem.messageType) {
      case NIMMessageType.text:
      case NIMMessageType.tip:
        return GradientCardView(
          elevation: PxUtil.instance.px_0,
          width: PxUtil.instance.screenWidth-PxUtil.instance.px_30,
          height: PxUtil.instance.px_44,
          alignment: Alignment.center,
          borderRadius: BorderRadius.circular(
              PxUtil.instance.px_8),
          child: Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: ColorUtil.instance.color_2F4072,
                borderRadius: BorderRadius.circular(
                    PxUtil.instance.px_8),
              ),
              padding: EdgeInsets.only(
                left: PxUtil.instance.px_12,
                right: PxUtil.instance.px_12,
                top: PxUtil.instance.px_5,
                bottom: PxUtil.instance.px_5,
              ),
              child: GradientTextWidget(
                txt: '${msgDataItem?.content??''}',
                gradient: LinearGradient(colors: [
                  ColorUtil.instance.color_AD2CF1,
                  ColorUtil.instance.color_7776FF,
                ]),
                txtStyle: TextStyle(
                  fontSize: PxUtil.instance.px_14,
                  color: ColorUtil.instance.color_white
                ),
              )
          ),
        );
        // return GradientCardView(child: Container(
        //   padding: EdgeInsets.only(
        //     left: PxUtil.instance.px_15,
        //     right: PxUtil.instance.px_15,
        //     top: PxUtil.instance.px_10,
        //     bottom: PxUtil.instance.px_10,
        //   ),
        //   decoration: BoxDecoration(
        //       color: ColorUtil.instance.color_4b5e91,
        //       borderRadius: BorderRadius.circular(PxUtil.instance.px_20)
        //   ),
        //   child: Text(
        //     '${msgDataItem?.content??''}',
        //     style: TextStyle(
        //         color: ColorUtil.instance.color_white,
        //         fontSize: PxUtil.instance.px_14
        //     ),
        //   ),
        // ));
      case NIMMessageType.audio:
        return ChatMessageAudioItem(
          message: msgDataItem,
        );
      case NIMMessageType.image:
        return ChatMessageImageItem(
          message: msgDataItem,
        );
      case NIMMessageType.video:
        return ChatMessageVideoItem(
          message: msgDataItem,
        );
      // case NIMMessageType.notification:
      //   if (messageItemBuilder?.notifyMessageBuilder != null) {
      //     return messageItemBuilder!.notifyMessageBuilder!(message.nimMessage);
      //   }
      //   return ChatKitMessageNotificationItem(message: message.nimMessage);
      // case NIMMessageType.tip:
      //   if (messageItemBuilder?.tipsMessageBuilder != null) {
      //     return messageItemBuilder!.tipsMessageBuilder!(message.nimMessage);
      //   }
      //   return ChatKitMessageTipsItem(message: message.nimMessage);
      default:
        return Container(
          padding: EdgeInsets.only(
            left: PxUtil.instance.px_15,
            right: PxUtil.instance.px_15,
            top: PxUtil.instance.px_10,
            bottom: PxUtil.instance.px_10,
          ),
          decoration: BoxDecoration(
              color: ColorUtil.instance.color_4b5e91,
              borderRadius: BorderRadius.circular(PxUtil.instance.px_20)
          ),
          child: Text(
            '${msgDataItem?.content??''}',
            style: TextStyle(
                color: ColorUtil.instance.color_white,
                fontSize: PxUtil.instance.px_14
            ),
          ),
        );
    }
  }
}
